window.dom = {
    create(string){//用于创建节点
        const container = document.createElement("template");
        container.innerHTML=string.trim();
        return container.content.firstChild;
    },
    after(node,node2) {//新增弟弟
        node.parentNode.insertBefore(node2,node.nextSibling);
    },
    before(node,node2) {//新增哥哥
        node.parentNode.insertBefore(node2,node);
    },
    append(parent,child) {//新增儿子
        parent.appendChild(child);
    },
    wrap(node,parent){//新增爸爸
        dom.before(node,parent);
        dom.append(parent,node);
    },
    //移除
    remove(node){
        node.parentNode.removeChild(node);
        return node;
    },
    empty(node) {
        const  {childNode} = node;
        const  array = [];
        let nextNode = node.firstChild;
        while (nextNode){
            array.push(dom.remove(nextNode));
            nextNode = node.firstChild;
        }
        return array;
    },
    attr(node,attrName,value){//重载
        if (arguments.length===3){
            node.setAttribute(name,value)
        }else if (arguments.length===2){
            return node.getAttribute(name)
        }
    },
    text(node,value){
        if (arguments.length===1){
            if ('innerText' in node){
                return node.innerText;
            }else {
                return node.textContent;
            }
        }else if (arguments.length===2){
            if ('innerText' in node){
                node.innerText = value;
            }else {
                node.textContent = value;
            }
        }
    },
    html(node,value){
        if (arguments.length===1){
           return node.innerHTML;
        }else if (arguments.length===2){
            node.innerHTML = value;
        }
    },
    style(node,name,value){
        if (arguments.length === 3){
            node.style[name] = value;
        }else if (arguments.length === 2){
            if (typeof name === 'string'){
                return node.style;
            }else if (name instanceof Object){
                for (let key in  name){
                    node.style[key] = name[key];
                }
            }
        }
    },
    class:{
        add(node,className){
            node.classList.add(className);
        },
        remove(node,className) {
            node.classList.remove(className);
        },
        has(node,className){
            return node.classList.contains(className);
        },
    },
    on(node,eventName,fn){
        node.addEventListener(eventName,fn);
    },
    off(node,eventName,fn){
        node.removeEventListener(eventName,fn)
    },
    find(selector,scope){
        return (scope || document).querySelectorAll(selector);
    },
    parent(node){
        return node.parentNode;
    },
    children(node){
        return node.children;
    },
    sibling(node){
        return Array.from(node.parentNode.children).filter(n=>n!==node)
    },
    next(node){
        let nextNode = node.nextSibling;
        while (nextNode.nodeType === 3 ){
            nextNode = nextNode.nextSibling;
        }
        return nextNode;
    },
    previous(node){
        let previousNode = node.previousSibling;
        while (previousNode.nodeType === 3 ){
            previousNode = previousNode.previousSibling;
        }
        return previousNode;
    },
    each(nodeList,fn){
        for (let i = 0; i < nodeList.length; i++) {
            fn.call(null,nodeList[i]);
        }
    },
    index(node){
        const  list = dom.children(node.parentNode);
        let i;
        for (i = 0; i < list.length; i++) {
            if (list[i] === node){
                break;
            }
        }
        return i+1;
    },
};


